import {request} from '/@/utils/service';
import {UserPageQuery} from "@fast-crud/fast-crud";
import {controlJournalStore} from "/@/stores/modules/controljournal";
import * as api from "/@/views/system/console/single/api";

// 处理期刊列表为表格对应的形式
export function handleJournalList(response: any) {
    const journalList: any = response.data;
    //console.log(journalList);
    let handledList = [];
    let index = 1;
    for (let i = 0; i < journalList.length; i++) {
        let journal = {
            code: '',
            name: '',
            checkedYears: [],
            id: index,
            status: 1,
            is_running: '',
        };
        journal.code = journalList[i].id;
        journal.name = journalList[i].journal;
        journal.is_running = journalList[i].is_running;
        handledList.push(journal);
        index++;
    }
    //console.log(handledList);
    return handledList;
}

// 部署爬取任务
export function deployCrawlTask(data: object) {
    const req = request({
        url: 'api/spider/agent/download',
        method: 'post',
        data: data
    })
    return req;
}

export function pauseCrawlTask() {
    return request({
        url: '',
        method: 'get'
    })
}

export function getAgentInfo() {
    return request({
        url: '/api/spider/info/agent',
        method: 'get',
    });
}

export function getAllLogs() {
    return request({
        url: '/api/spider/info/allLogs',
        method: 'get',
    });
}

export function getStatistic(id: BigInt) {
    return request({
        url: '/api/spider/info/statistic',
        method: 'get',
        params: {
            id,
        },
    });
}

export function getAllAgent(pageSize: BigInt) {
    return request({
        url: '/api/spider/info/agent',
        method: 'get',
        params: {
            key: '',
            pageSize: pageSize,
            status: true,
        }
    })
}

//查询正在运行的智能体
export function getAllRunAgent(pageSize: BigInt) {
    return request({
        url: '/api/spider/info/agent',
        method: 'get',
        params: {
            key: '',
            pageSize: pageSize,
            status: true,
            is_running: true,
        }
    })
}

export function getSpeedProgress(agent_sum: BigInt, last_progress: number) {
    return request({
        url: '/api/spider/info/crawlSpeedProgress',
        method: 'get',
        params: {
            agent_sum: agent_sum,
            last_progress: last_progress,
        }
    })
}

export function SwitchControl(id: BigInt, action: String) {
    return request({
        url: '/api/spider/agent/control',
        method: 'get',
        params: {
            id,
            action
        },
    });
}
